﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExternalConnections.CUiECEditOpcConnectionForm
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExternalConnections.Res;
using EngineeringInternalExtension;
using EngineeringInternalExtension.CommandParameters;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.ExternalConnections;

internal class CUiECEditOpcConnectionForm : TxForm
{
  private CApECExternalConnectionsManager m_dataMgr;
  private CApECOpcConnectionFormParams m_params;
  private int m_suspendEnablersCounter;
  private IContainer components;
  private Label m_labelName;
  private Label m_labelOpcServer;
  private Label m_labelItemHeader;
  private Label m_labelMapSignalsBy;
  private TextBox m_textBoxName;
  private TextBox m_textBoxOpcServer;
  private TextBox m_textBoxItemHeader;
  private ComboBox m_comboBoxMapSignalsBy;
  private Button m_buttonOK;
  private Button m_buttonCancel;
  private Button m_buttonBrowseOpcServer;

  internal CUiECEditOpcConnectionForm(CApECExternalConnectionsManager mgr)
  {
    this.InitializeComponent();
    this.m_dataMgr = mgr;
    this.RegisterForControlEvents();
  }

  internal DialogResult ShowDialog(CApECOpcConnectionFormParams formParams)
  {
    this.InitMode(formParams);
    return ((Form) this).ShowDialog();
  }

  private void InitMode(CApECOpcConnectionFormParams formParams)
  {
    this.m_params = formParams;
    this.SuspendUiEnablers();
    try
    {
      switch (this.m_params.DialogMode)
      {
        case CApECOpcConnectionFormParams.eMode.Edit:
          this.InitUiByData(this.m_params.ConnectionToEdit.Data, false);
          ((Control) this).Text = CUiECExternalConnectionsResourceTable.EDIT_OPC_CONNECTION_DLG_CAPTION;
          this.m_textBoxName.Focus();
          this.m_textBoxName.SelectAll();
          break;
        case CApECOpcConnectionFormParams.eMode.Create:
          this.InitUiByData((ITxPlcExternalConnectionDataEx) new TxPlcOpcConnectionDataEx(), false);
          ((Control) this).Text = CUiECExternalConnectionsResourceTable.ADD_OPC_CONNECTION_DLG_CAPTION;
          this.m_textBoxName.Focus();
          break;
      }
    }
    finally
    {
      this.ResumeUiEnablers();
    }
  }

  private void InitUiByData(ITxPlcExternalConnectionDataEx data, bool updateEnablers)
  {
    IApECExternalConnectionDataConvertor instance = this.m_params.DataConvertor.CreateInstance(data);
    this.m_textBoxName.Text = instance.Name as string;
    this.m_textBoxOpcServer.Text = instance.OpcServer as string;
    this.m_textBoxItemHeader.Text = instance.ItemHeader as string;
    this.m_comboBoxMapSignalsBy.Items.Clear();
    object[] signalsByOptions = instance.MapSignalsByOptions;
    if (signalsByOptions != null && signalsByOptions.Length != 0)
    {
      this.m_comboBoxMapSignalsBy.Items.AddRange(signalsByOptions);
      this.m_comboBoxMapSignalsBy.SelectedItem = instance.MapSignalsBy;
    }
    if (!updateEnablers)
      return;
    this.UpdateAllEnablers();
  }

  private void InitServerByData(ITxPlcExternalConnectionDataEx data, bool updateEnablers)
  {
    this.m_textBoxOpcServer.Text = this.m_params.DataConvertor.CreateInstance(data).OpcServer as string;
    if (this.m_textBoxName.Text.Trim().Length == 0)
      this.m_textBoxName.Text = this.m_textBoxOpcServer.Text;
    if (!updateEnablers)
      return;
    this.UpdateAllEnablers();
  }

  private void InitItemHeaderByData(ITxPlcExternalConnectionDataEx data, bool updateEnablers)
  {
    this.m_textBoxItemHeader.Text = this.m_params.DataConvertor.CreateInstance(data).ItemHeader as string;
    if (!updateEnablers)
      return;
    this.UpdateAllEnablers();
  }

  private ITxPlcExternalConnectionDataEx RetrieveDataFromUi()
  {
    IApECExternalConnectionDataConvertor instance = this.m_params.DataConvertor.CreateInstance((ITxPlcExternalConnectionDataEx) new TxPlcOpcConnectionDataEx());
    instance.Name = (object) this.m_textBoxName.Text;
    instance.OpcServer = (object) this.m_textBoxOpcServer.Text;
    instance.ItemHeader = (object) this.m_textBoxItemHeader.Text;
    instance.MapSignalsBy = this.m_comboBoxMapSignalsBy.SelectedItem;
    return instance.Data;
  }

  private void RegisterForControlEvents()
  {
    this.m_buttonOK.Click += new EventHandler(this.m_buttonOK_Click);
    this.m_textBoxName.TextChanged += new EventHandler(this.m_textBoxName_TextChanged);
    this.m_textBoxOpcServer.TextChanged += new EventHandler(this.m_textBoxOpcServer_TextChanged);
    this.m_buttonBrowseOpcServer.Click += new EventHandler(this.m_buttonBrowseOpcServer_Click);
    this.m_textBoxItemHeader.TextChanged += new EventHandler(this.m_textBoxItemHeader_TextChanged);
    this.m_comboBoxMapSignalsBy.SelectedIndexChanged += new EventHandler(this.m_comboBoxMapSignalsBy_SelectedIndexChanged);
  }

  private void UnRegisterForControlEvents()
  {
    this.m_buttonOK.Click -= new EventHandler(this.m_buttonOK_Click);
    this.m_textBoxName.TextChanged -= new EventHandler(this.m_textBoxName_TextChanged);
    this.m_textBoxOpcServer.TextChanged -= new EventHandler(this.m_textBoxOpcServer_TextChanged);
    this.m_buttonBrowseOpcServer.Click -= new EventHandler(this.m_buttonBrowseOpcServer_Click);
    this.m_textBoxItemHeader.TextChanged -= new EventHandler(this.m_textBoxItemHeader_TextChanged);
    this.m_comboBoxMapSignalsBy.SelectedIndexChanged -= new EventHandler(this.m_comboBoxMapSignalsBy_SelectedIndexChanged);
  }

  private bool EnablersSuspended() => this.m_suspendEnablersCounter > 0;

  private void SuspendUiEnablers() => ++this.m_suspendEnablersCounter;

  private void ResumeUiEnablers()
  {
    if (this.EnablersSuspended())
      --this.m_suspendEnablersCounter;
    if (this.EnablersSuspended())
      return;
    this.UpdateAllEnablers();
  }

  private void UpdateAllEnablers()
  {
    if (this.EnablersSuspended())
      return;
    this.UpdateEnabler_ButtonOK();
  }

  private void UpdateEnabler_ButtonOK()
  {
    if (this.EnablersSuspended())
      return;
    this.m_buttonOK.Enabled = this.m_textBoxName.Text.Trim().Length > 0 && this.m_textBoxOpcServer.Text.Trim().Length > 0;
  }

  protected virtual void OnClosing(CancelEventArgs e)
  {
    if (((Form) this).DialogResult == DialogResult.OK)
    {
      ITxPlcExternalConnectionDataEx connectionDataEx = this.RetrieveDataFromUi();
      switch (this.m_params.DialogMode)
      {
        case CApECOpcConnectionFormParams.eMode.Edit:
          CApECExternalConnectionsManager.eSetConnectionDataFailiureReason failiureReason1;
          if (!this.m_dataMgr.CanSetConnectionData(this.m_params.ConnectionToEdit, connectionDataEx, out failiureReason1))
          {
            e.Cancel = true;
            if (failiureReason1 == CApECExternalConnectionsManager.eSetConnectionDataFailiureReason.DuplicatedName)
            {
              int num = (int) TxMessageBox.ShowModal(CUiECExternalConnectionsResourceTable.ERR_MSG_DUPLICATED_CONNECTION_NAME, ((Control) this).Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
              break;
            }
            break;
          }
          break;
        case CApECOpcConnectionFormParams.eMode.Create:
          CApECExternalConnectionsManager.eCreateConnectionFailiureReason failiureReason2;
          if (!this.m_dataMgr.CanCreateConnectionWithData(connectionDataEx, out failiureReason2))
          {
            e.Cancel = true;
            if (failiureReason2 == CApECExternalConnectionsManager.eCreateConnectionFailiureReason.DuplicatedName)
            {
              int num = (int) TxMessageBox.ShowModal(CUiECExternalConnectionsResourceTable.ERR_MSG_DUPLICATED_CONNECTION_NAME, ((Control) this).Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
              break;
            }
            break;
          }
          break;
      }
      if (!e.Cancel)
        this.m_params.DataToApply = connectionDataEx;
      else
        ((Form) this).DialogResult = DialogResult.Cancel;
    }
    base.OnClosing(e);
  }

  private void m_buttonOK_Click(object sender, EventArgs e)
  {
    ((Form) this).DialogResult = DialogResult.OK;
    if (((Form) this).Modal)
      return;
    ((Form) this).Close();
  }

  private void m_buttonCancel_Click(object sender, EventArgs e)
  {
    ((Form) this).DialogResult = DialogResult.Cancel;
    if (((Form) this).Modal)
      return;
    ((Form) this).Close();
  }

  private void m_textBoxName_TextChanged(object sender, EventArgs e)
  {
    this.UpdateEnabler_ButtonOK();
  }

  private void m_textBoxOpcServer_TextChanged(object sender, EventArgs e)
  {
    this.UpdateEnabler_ButtonOK();
  }

  private void m_textBoxItemHeader_TextChanged(object sender, EventArgs e)
  {
  }

  private void m_comboBoxMapSignalsBy_SelectedIndexChanged(object sender, EventArgs e)
  {
  }

  private void m_buttonBrowseOpcServer_Click(object sender, EventArgs e)
  {
    ITxPlcExternalConnectionDataEx connectionDataEx1 = this.RetrieveDataFromUi();
    if (!(connectionDataEx1 != null & connectionDataEx1 is TxPlcOpcConnectionDataEx))
      return;
    TxPlcOpcConnectionDataEx connectionDataEx2 = connectionDataEx1 as TxPlcOpcConnectionDataEx;
    TxOpcServerCommandParametersEx commandParametersEx = new TxOpcServerCommandParametersEx(connectionDataEx2.IsRemoteServer, connectionDataEx2.ServerId, connectionDataEx2.ServerPath, connectionDataEx2.ItemHeader);
    bool flag1;
    try
    {
      flag1 = TxApplication.CommandsManager.ExecuteCommand("PlcCOPCSimulation.OPCBrowser", (object) commandParametersEx);
    }
    catch (TxCommandIdentifierDoesNotExistException ex)
    {
      flag1 = false;
    }
    if (!flag1)
      return;
    bool flag2 = false;
    bool flag3 = false;
    if (commandParametersEx.IsRemoteServer != connectionDataEx2.IsRemoteServer)
    {
      connectionDataEx2.IsRemoteServer = commandParametersEx.IsRemoteServer;
      flag2 = true;
    }
    if (commandParametersEx.ServerId != connectionDataEx2.ServerId)
    {
      connectionDataEx2.ServerId = commandParametersEx.ServerId;
      flag2 = true;
    }
    if (commandParametersEx.ServerPath != connectionDataEx2.ServerPath)
    {
      connectionDataEx2.ServerPath = commandParametersEx.ServerPath;
      flag2 = true;
    }
    if (commandParametersEx.ItemHeader != connectionDataEx2.ItemHeader)
    {
      connectionDataEx2.ItemHeader = commandParametersEx.ItemHeader;
      flag3 = true;
    }
    if (!(flag2 | flag3))
      return;
    this.SuspendUiEnablers();
    ITxPlcExternalConnectionDataEx data = (ITxPlcExternalConnectionDataEx) connectionDataEx2;
    try
    {
      if (flag2)
        this.InitServerByData(data, false);
      if (!flag3)
        return;
      this.InitItemHeaderByData(data, false);
    }
    finally
    {
      this.ResumeUiEnablers();
    }
  }

  private void CUiECEditOpcConnectionForm_Load(object sender, EventArgs e)
  {
    ((Control) this).MinimumSize = new Size(363, 198);
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiECEditOpcConnectionForm));
    this.m_labelName = new Label();
    this.m_labelOpcServer = new Label();
    this.m_labelItemHeader = new Label();
    this.m_labelMapSignalsBy = new Label();
    this.m_textBoxName = new TextBox();
    this.m_textBoxOpcServer = new TextBox();
    this.m_textBoxItemHeader = new TextBox();
    this.m_comboBoxMapSignalsBy = new ComboBox();
    this.m_buttonOK = new Button();
    this.m_buttonCancel = new Button();
    this.m_buttonBrowseOpcServer = new Button();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_labelName, "m_labelName");
    this.m_labelName.Name = "m_labelName";
    componentResourceManager.ApplyResources((object) this.m_labelOpcServer, "m_labelOpcServer");
    this.m_labelOpcServer.Name = "m_labelOpcServer";
    componentResourceManager.ApplyResources((object) this.m_labelItemHeader, "m_labelItemHeader");
    this.m_labelItemHeader.Name = "m_labelItemHeader";
    componentResourceManager.ApplyResources((object) this.m_labelMapSignalsBy, "m_labelMapSignalsBy");
    this.m_labelMapSignalsBy.Name = "m_labelMapSignalsBy";
    componentResourceManager.ApplyResources((object) this.m_textBoxName, "m_textBoxName");
    this.m_textBoxName.Name = "m_textBoxName";
    componentResourceManager.ApplyResources((object) this.m_textBoxOpcServer, "m_textBoxOpcServer");
    this.m_textBoxOpcServer.Name = "m_textBoxOpcServer";
    componentResourceManager.ApplyResources((object) this.m_textBoxItemHeader, "m_textBoxItemHeader");
    this.m_textBoxItemHeader.Name = "m_textBoxItemHeader";
    componentResourceManager.ApplyResources((object) this.m_comboBoxMapSignalsBy, "m_comboBoxMapSignalsBy");
    this.m_comboBoxMapSignalsBy.DropDownStyle = ComboBoxStyle.DropDownList;
    this.m_comboBoxMapSignalsBy.FormattingEnabled = true;
    this.m_comboBoxMapSignalsBy.Name = "m_comboBoxMapSignalsBy";
    componentResourceManager.ApplyResources((object) this.m_buttonOK, "m_buttonOK");
    this.m_buttonOK.Name = "m_buttonOK";
    this.m_buttonOK.UseVisualStyleBackColor = true;
    componentResourceManager.ApplyResources((object) this.m_buttonCancel, "m_buttonCancel");
    this.m_buttonCancel.DialogResult = DialogResult.Cancel;
    this.m_buttonCancel.Name = "m_buttonCancel";
    this.m_buttonCancel.UseVisualStyleBackColor = true;
    this.m_buttonCancel.Click += new EventHandler(this.m_buttonCancel_Click);
    componentResourceManager.ApplyResources((object) this.m_buttonBrowseOpcServer, "m_buttonBrowseOpcServer");
    this.m_buttonBrowseOpcServer.Name = "m_buttonBrowseOpcServer";
    this.m_buttonBrowseOpcServer.UseVisualStyleBackColor = true;
    ((Form) this).AcceptButton = (IButtonControl) this.m_buttonOK;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleDimensions = new SizeF(6f, 13f);
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Form) this).CancelButton = (IButtonControl) this.m_buttonCancel;
    ((Control) this).Controls.Add((Control) this.m_buttonBrowseOpcServer);
    ((Control) this).Controls.Add((Control) this.m_buttonCancel);
    ((Control) this).Controls.Add((Control) this.m_buttonOK);
    ((Control) this).Controls.Add((Control) this.m_comboBoxMapSignalsBy);
    ((Control) this).Controls.Add((Control) this.m_textBoxItemHeader);
    ((Control) this).Controls.Add((Control) this.m_textBoxOpcServer);
    ((Control) this).Controls.Add((Control) this.m_textBoxName);
    ((Control) this).Controls.Add((Control) this.m_labelMapSignalsBy);
    ((Control) this).Controls.Add((Control) this.m_labelItemHeader);
    ((Control) this).Controls.Add((Control) this.m_labelOpcServer);
    ((Control) this).Controls.Add((Control) this.m_labelName);
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiECEditOpcConnectionForm);
    ((Form) this).ShowIcon = false;
    ((Form) this).SizeGripStyle = SizeGripStyle.Hide;
    ((Form) this).Load += new EventHandler(this.CUiECEditOpcConnectionForm_Load);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
